package project1;
import java.sql.*;
import java.lang.Class;
import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;

/**
 * Mimics Gradience
 * @author Hayden Willis and Alton Walston
 *
 */


public class mainproject1 {
	static final String jdbcURL 
	//= "jdbc:oracle:thin:@//orca.csc.ncsu.edu:1521/ORCL.WORLD";
	= "jdbc:oracle:thin:@//orca.csc.ncsu.edu:1523/ORCL";
	
	private static final String QUESTIONS_TABLE = "CREATE TABLE Questions (seed_Num INT, difficulty INT, "
			+"TopicID INT REFERENCES topics(TopicID), content VARCHAR(100), questionID VARCHAR(2) PRIMARY KEY"
			+")";//topics
			
	private static final String ASSESSMENTS_TABLE = "CREATE TABLE assessments(Attempted INT,"
			+"AssignmentID INT PRIMARY KEY,  Questions VARCHAR(200), StartDate VARCHAR(10), EndDate VARCHAR(10), "
			+"ScoreSelection VARCHAR(20), RetriesLeft INT, PointsPerQuestion INT, Name VARCHAR(10), "
			+"PointsOPIQ INT, StudentID VARCHAR(10) REFERENCES student(StudentID), Answers VARCHAR(200), StudentAnswers VARCHAR(200),"
			+"Score INT, ProfessorID VARCHAR(20) REFERENCES Professor(ProfessorID), NumberOfQuestions INT)";//professor,students
	
	private static final String PROFESSOR_TABLE = "CREATE TABLE Professor(Teaches VARCHAR(10), "
			+"Password VARCHAR(10), ProfessorID VARCHAR(20), PRIMARY KEY(ProfessorID))";//none
	
	private static final String CORRECT_ANSWERS_TABLE = "CREATE TABLE correctAnswers (seed_Num INT, "
			+"explanation VARCHAR(100), content VARCHAR(100), questionID VARCHAR(2) REFERENCES Questions(questionID), cAnswerID INT PRIMARY KEY)";//questions
	
	private static final String INCORRECT_ANSWERS_TABLE = "CREATE TABLE incorrectAnswers (seed_Num INT,"
			+"hint VARCHAR(100), content VARCHAR(100), questionID VARCHAR(2) REFERENCES Questions(questionID), iAnswerID INT PRIMARY KEY)";//questions
	
	private static final String STUDENT_TABLE = "CREATE TABLE student (password VARCHAR(30), "
			+"enrolledIn VARCHAR(200), studentID VARCHAR(30) PRIMARY KEY)";//none
	
	private static final String COURSES_TABLE = "CREATE TABLE courses(CoursesName VARCHAR(50), "
			+"CourseID VARCHAR(10), Token VARCHAR(20), EndDate VARCHAR(10), StartDate VARCHAR(10), "
			+"Students VARCHAR(100), ProfessorID VARCHAR(20) REFERENCES Professor(ProfessorID), Topic VARCHAR(5), PRIMARY KEY(CourseID))";//professor
	
	private static final String STUDENT_HOMEWORK_TABLE = "CREATE TABLE studentsHomework(studentID VARCHAR(30) REFERENCES student(studentID), AssignmentID INT REFERENCES assessments(AssignmentID))";
	//student, assignment
	private static final String TOPIC_TABLE = "CREATE TABLE topics(TopicID INT PRIMARY KEY,name VARCHAR(50))";//none
	
	public static void main(String args[]){
		try {

        // Load the driver. This creates an instance of the driver
	    // and calls the registerDriver method to make Oracle Thin
	    // driver available to clients.

        Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
        String jdbcURL = "jdbc:oracle:thin:@//orca.csc.ncsu.edu:1523/ORCL";
	    String user = "hrwilli2";	// For example, "jsmith"
	    String passwd = "000963710";	// Your 9 digit student ID number


        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {

		// Get a connection from the first driver in the
		// DriverManager list that recognizes the URL jdbcURL
                
		conn = DriverManager.getConnection(jdbcURL, user, passwd);
                
		conn = DriverManager.getConnection(jdbcURL, user, passwd);

		// Create a statement object that will be sending your
		// SQL statements to the DBMS

		stmt = conn.createStatement();

		stmt.executeUpdate("DROP TABLE studentsHomework");
		stmt.executeUpdate("DROP TABLE courses");
		stmt.executeUpdate("DROP TABLE incorrectAnswers");
		stmt.executeUpdate("DROP TABLE correctAnswers");
		stmt.executeUpdate("DROP TABLE Questions");
		stmt.executeUpdate("DROP TABLE assessments");
		stmt.executeUpdate("DROP TABLE student");
		stmt.executeUpdate("DROP TABLE topics");
		stmt.executeUpdate("DROP TABLE Professor");
		
		stmt.executeUpdate(TOPIC_TABLE);
		stmt.executeUpdate(STUDENT_TABLE);
		stmt.executeUpdate(PROFESSOR_TABLE);
		stmt.executeUpdate(COURSES_TABLE);
		stmt.executeUpdate(QUESTIONS_TABLE);
		stmt.executeUpdate(ASSESSMENTS_TABLE);
		stmt.executeUpdate(CORRECT_ANSWERS_TABLE);
		stmt.executeUpdate(INCORRECT_ANSWERS_TABLE);
		stmt.executeUpdate(STUDENT_HOMEWORK_TABLE);
		
		
		stmt.executeUpdate("INSERT INTO Professor " +
				   "VALUES ('CSC440', 'password', '123kogan')");
		stmt.executeUpdate("INSERT INTO Professor " +
				   "VALUES ('CSC541', 'password', '123rchirkova')");
		stmt.executeUpdate("INSERT INTO Professor " +
				   "VALUES ('CSC501', 'password', '123rmueller')");
		
		
		stmt.executeUpdate("INSERT INTO student " +
				   "VALUES ('123bud', 'CSC440,', 'Ssbudha')");
		stmt.executeUpdate("INSERT INTO student " +
				   "VALUES ('123kan', 'CSC440,', 'Sskanit')");
		stmt.executeUpdate("INSERT INTO student " +
				   "VALUES ('123hol', 'CSC440,CSC501,', 'Agholak')");
		stmt.executeUpdate("INSERT INTO student " +
				   "VALUES ('123jos', 'CSC440,', 'Rjoseph')");
		stmt.executeUpdate("INSERT INTO student " +
				   "VALUES ('123bir', '', 'Tbirajd')");
		
		
		stmt.executeUpdate("INSERT INTO topics " +
				   "VALUES (1, 'Database Fundamentals')");
		stmt.executeUpdate("INSERT INTO topics " +
				   "VALUES (2, 'Security and Authorization')");
		stmt.executeUpdate("INSERT INTO topics " +
				   "VALUES (3, 'ER Design and other topics')");
		stmt.executeUpdate("INSERT INTO topics " +
				   "VALUES (4, 'Binary search trees and Btrees')");
		stmt.executeUpdate("INSERT INTO topics " +
				   "VALUES (5, 'Hashing')");
		stmt.executeUpdate("INSERT INTO topics " +
				   "VALUES (6, 'Files and indexing and other topics')");
		stmt.executeUpdate("INSERT INTO topics " +
				   "VALUES (7, 'Processes and Threads')");
		stmt.executeUpdate("INSERT INTO topics " +
				   "VALUES (8, 'Memory Organization')");
		stmt.executeUpdate("INSERT INTO topics " +
				   "VALUES (9, 'Deadlocks and other topics')");
		
		//need ta ids
		stmt.executeUpdate("INSERT INTO courses " +
				   "VALUES ('Database Systems', 'CSC440', 'CSC440SPR12', '2012-05-10',"
				   +" '2012-01-01', 'ssbudha,sskanit,agholak,rjoseph','123kogan','1,2,3')");//Kemafor Ogan(id)=123kogan
		stmt.executeUpdate("INSERT INTO courses " +
				   "VALUES ('Advanced Data Structures', 'CSC541', 'CSC541FLL11', '2011-12-15',"
				   +" '2011-08-01', '','123rchirkova','4,5,6')");//Rada Chirkova(id)=123rchirkova
		stmt.executeUpdate("INSERT INTO courses " +
				   "VALUES ('Operating Systems', 'CSC501', 'CSC501SPR12', '2012-05-10',"
				   +" '2012-01-01', 'agholak,123kogan','123rmueller','7,8,9')");//Dr. R. Mueller(id)=123rmueller
		
		
		stmt.executeUpdate("INSERT INTO assessments " +
				   "VALUES (0,0,'','2012-02-12','2012-03-01','first attempt',2,3,'Homework 1',1,'','','',0,'123kogan',1)");
		stmt.executeUpdate("INSERT INTO assessments " +
				   "VALUES (0,1,'Q1,Q2','2012-03-12','2012-04-01','average',1,5,'Homework 2',2,'','','',0,'123kogan',2)");
		
		stmt.executeUpdate("INSERT INTO Questions VALUES(1, 1, 1, 'Question 1','Q1')");
		stmt.executeUpdate("INSERT INTO Questions VALUES(2, 1, 1, 'Question 2','Q2')");
		stmt.executeUpdate("INSERT INTO Questions VALUES(3, 1, 1, 'Question 3','Q3')");
		
		//correct answer
		stmt.executeUpdate("INSERT INTO correctAnswers " +
				   "VALUES (1, 'Explanation 1.1','Correct 1.1','Q1',1)");
		stmt.executeUpdate("INSERT INTO correctAnswers " +
				   "VALUES (2, 'Explanation 1.2','Correct 1.2','Q1',2)");
		stmt.executeUpdate("INSERT INTO correctAnswers " +
				   "VALUES (3, 'Explanation 1.3','Correct 1.3','Q1',3)");
		stmt.executeUpdate("INSERT INTO correctAnswers " +
				   "VALUES (1, 'Explanation 2.1','Correct 2.1','Q2',4)");
		stmt.executeUpdate("INSERT INTO correctAnswers " +
				   "VALUES (2, 'Explanation 2.2','Correct 2.2','Q2',5)");
		stmt.executeUpdate("INSERT INTO correctAnswers " +
				   "VALUES (3, 'Explanation 2.3','Correct 2.3','Q2',6)");
		stmt.executeUpdate("INSERT INTO correctAnswers " +
				   "VALUES (1, 'Explanation 3.1','Correct 3.1','Q3',7)");
		stmt.executeUpdate("INSERT INTO correctAnswers " +
				   "VALUES (2, 'Explanation 3.2','Correct 3.2','Q3',8)");
		stmt.executeUpdate("INSERT INTO correctAnswers " +
				   "VALUES (3, 'Explanation 3.3','Correct 3.3','Q3',9)");
		//incorrect q1
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (1, 'Hint 1.1','Incorrect 1.1','Q1',1)");
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (2, 'Hint 1.2','Incorrect 1.2','Q1',2)");
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (3, 'Hint 1.3','Incorrect 1.3','Q1',3)");
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (4, 'Hint 1.4','Incorrect 1.4','Q1',4)");
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (5, 'Hint 1.5','Incorrect 1.5','Q1',5)");
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (6, 'Hint 1.6','Incorrect 1.6','Q1',6)");
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (7, 'Hint 1.7','Incorrect 1.7','Q1',7)");
		//incorrect q2
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (1, 'Hint 2.1','Incorrect 2.1','Q2',8)");
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (2, 'Hint 2.2','Incorrect 2.2','Q2',9)");
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (3, 'Hint 2.3','Incorrect 2.3','Q2',10)");
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (4, 'Hint 2.4','Incorrect 2.4','Q2',11)");
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (5, 'Hint 2.5','Incorrect 2.5','Q2',12)");
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (6, 'Hint 2.6','Incorrect 2.6','Q2',13)");
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (7, 'Hint 2.7','Incorrect 2.7','Q2',14)");
		//incorrect q3
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (1, 'Hint 3.1','Incorrect 3.1','Q3',15)");
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (2, 'Hint 3.2','Incorrect 3.2','Q3',16)");
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (3, 'Hint 3.3','Incorrect 3.3','Q3',17)");
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (4, 'Hint 3.4','Incorrect 3.4','Q3',18)");
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (5, 'Hint 3.5','Incorrect 3.5','Q3',19)");
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (6, 'Hint 3.6','Incorrect 3.6','Q3',20)");
		stmt.executeUpdate("INSERT INTO incorrectAnswers " +
				   "VALUES (7, 'Hint 3.7','Incorrect 3.7','Q3',21)");
		
		
		String userID = "";
		String courseID = "";
		String userType = "";
		SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//dd/MM/yyyy
		Date now = new Date();
		String strDate = sdfDate.format(now);
		String currentDate = strDate.substring(0,10);
		//main code for user interface
		boolean login = true;
		while(login){
			int choice = start();
			switch(choice){
				case 1://login page
				
					String[] userLogin = login();
					rs = stmt.executeQuery("SELECT COUNT(*) FROM student WHERE studentID='"+userLogin[0]+"', password='"+userLogin[1]+"'");
					int n = rs.getInt("COUNT(*)");
					if(n==1){
						//found user
						userID = userLogin[0];
						userType = "student";
					} else {
						rs = stmt.executeQuery("SELECT COUNT(*) FROM Professor WHERE ProfessorID='"+userLogin[0]+"', Password='"+userLogin[1]+"'");
						n = rs.getInt("COUNT(*)");
						if(n==1){
							//found user
							userID = userLogin[0];
							userType = "professor";
						}//else check ta table
						
					} if(n!=1) {
						System.out.println("Login Incorrect");
						break;
					}
					
					int loggedin = loggedInChooseCourse();
					switch(loggedin){
	    				case 1://select a course page
	    					if(userType=="student"){
	    						rs = stmt.executeQuery("SELECT enrolledIn FROM student WHERE studentID='"+userID+"'");
	    						String enrolled = rs.getString("enrolledIn");
	    						char[] enrolledIn = enrolled.toCharArray();
	    						int i=0;
	    						int j=0;
	    						int numOfClasses=0;
	    						int[] index = new int[20];
	    						for(i=0; i<enrolledIn.length; i++){//count number of classes enrolled in
	    							if(i==0&&enrolledIn[0]==',')
	    								i++;
	    							if(enrolledIn[i]==','){
	    								numOfClasses++;
	    								index[j]=i;
	    								j++;
	    							}
	    						}
	    						if(numOfClasses==0){//not enrolled in any courses
	    							System.out.println("You aren't enrolled an any class");
	    							break;
	    						}
	    						j=0;
	    						int d=0;
	    						String[] crass = new String[numOfClasses];
	    						for(i=0; i<numOfClasses; i++){//makes strings of class ids
	    							crass[j]=enrolled.substring(d, index[i]-1);
	    							d=index[i]+1;
	    							j++;
	    						}
	    						String[] names = new String[numOfClasses];
	    						for(i=0; i<numOfClasses; i++){
	    							rs = stmt.executeQuery("SELECT CoursesName FROM courses WHERE CourseID='"+crass[i]+"'");
	    							names[i] = rs.getString("CoursesName");
	    						}
	    						
	    						courseID = selectCourse(crass, names);
	    						//at this point we are logged in as a student and selected our course
	    						
	    						int courseOptions = getCourseOptionsStu();
	    						
	    						switch(courseOptions){
	    							case 1://view scores
	    								
	    								break;
	    							case 2://attempt homework
	    								
	    								break;
	    							case 3://view past submissions
	    								
	    								break;
	    							case 4://back
	    								break;
	    						}
	    						
	    						
	    						
	    					} else if(userType=="professor"){
	    						rs = stmt.executeQuery("SELECT Teaches FROM Professor WHERE ProfessorID='"+userID+"'");
	    						String enrolled = rs.getString("Teaches");
	    						char[] enrolledIn = enrolled.toCharArray();
	    						int i=0;
	    						int j=0;
	    						int numOfClasses=0;
	    						int[] index = new int[20];
	    						for(i=0; i<enrolledIn.length; i++){//count number of classes enrolled in
	    							if(i==0&&enrolledIn[0]==',')
	    								i++;
	    							if(enrolledIn[i]==','){
	    								numOfClasses++;
	    								index[j]=i;
	    								j++;
	    							}
	    						}
	    						if(numOfClasses==0){//not enrolled in any courses
	    							System.out.println("You aren't teaching an any class");
	    							break;
	    						}
	    						j=0;
	    						int d=0;
	    						String[] crass = new String[numOfClasses];
	    						for(i=0; i<numOfClasses; i++){//makes strings of class ids
	    							crass[j]=enrolled.substring(d, index[i]-1);
	    							d=index[i]+1;
	    							j++;
	    						}
	    						String[] names = new String[numOfClasses];
	    						for(i=0; i<numOfClasses; i++){
	    							rs = stmt.executeQuery("SELECT CoursesName FROM courses WHERE CourseID='"+crass[i]+"'");
	    							names[i] = rs.getString("CoursesName");
	    						}
	    						
	    						courseID = selectCourse(crass, names);
	    						//at this point we are logged in as a professor and selected our course
	    						
	    						int courseOptions = getCourseOptionsPro();
	    						
	    						switch(courseOptions){
	    							case 1://add homework
	    								String[] info = getNewAssignmentInfo();
	    								stmt.executeUpdate("INSERT INTO assessments " +
	    										   "VALUES (0,3,'','"+info[0]+"','"+info[1]+"','"+info[3]+"',"+info[2]+","+info[5]+",'"+info[7]+"',"+info[6]+",'','','',0,'"+userID+"',"+info[4]+")");
	    								//assignment#=3 (fix)
	    								break;
	    							case 2://edit homework
	    								rs = stmt.executeQuery("SELECT COUNT(*) FROM assessments WHERE ProfessorID='"+userID+"'");
	    								int numOfAssess = rs.getInt("COUNT(*)");
	    								rs = stmt.executeQuery("SELECT Name FROM assessments WHERE ProfessorID='"+userID+"'");
	    								String[] edit = new String[numOfAssess];
	    								i=0;
	    								while(rs.next()){
	    									edit[i]=rs.getString("Name");
	    									i++;
	    								}
	    								String editHomework = displayHomework(edit);
	    								
	    								int editChoice = getEditChoice();
	    								switch(editChoice){
	    									case 1://edit start date
	    										BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	    										System.out.println("Enter New Start Date (YYYY-MM-DD):");
	    										String newStart = in.readLine();
	    										stmt.executeUpdate("UPDATE assessments SET StartDate='"+newStart+"' WHERE ProfessorID='"+userID+"', AssignmentID='"+editHomework+"'");
	    										break;
	    									case 2://edit end date
	    										BufferedReader in1 = new BufferedReader(new InputStreamReader(System.in));
	    										System.out.println("Enter New End Date (YYYY-MM-DD):");
	    										String newEnd = in1.readLine();
	    										stmt.executeUpdate("UPDATE assessments SET EndDate='"+newEnd+"' WHERE ProfessorID='"+userID+"', AssignmentID='"+editHomework+"'");
	    										break;
	    									case 3://number of attempts
	    										BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in));
	    										System.out.println("Enter New Number of Attempts:");
	    										int newAttempt = in2.read();
	    										stmt.executeUpdate("UPDATE assessments SET Attempted='"+newAttempt+"' WHERE ProfessorID='"+userID+"', AssignmentID='"+editHomework+"'");
	    										break;
	    									case 4://score selection
	    										System.out.println("New Score Selections can ONLY be: 'first attempt', 'last attempt', 'maximum of all attempts', 'average of all attempts'");
	    										BufferedReader in3 = new BufferedReader(new InputStreamReader(System.in));
	    										String inp = in3.readLine();
	    										while(inp!="first attempt" || inp!="last attempt" || inp!="aximum of all attempts" || inp!="average of all attempts"){
	    											System.out.println("Invalid Option, Must Input 'first attempt', 'last attempt', 'maximum of all attempts', or 'average of all attempts'");
	    											inp = in3.readLine();
	    										}
	    										stmt.executeUpdate("UPDATE assessments SET ScoreSelection='"+inp+"' WHERE ProfessorID='"+userID+"', AssignmentID='"+editHomework+"'");
	    										break;
	    									case 5://number of questions
	    										System.out.println("Input the New Number of Questions: ");
	    										BufferedReader in4 = new BufferedReader(new InputStreamReader(System.in));
	    										int inpu = in4.read();
	    										stmt.executeUpdate("UPDATE assessments SET NumberOfQuestions='"+inpu+"' WHERE ProfessorID='"+userID+"', AssignmentID='"+editHomework+"'");
	    										break;
	    									case 6://points per correct answer
	    										System.out.println("Input New Points Per Correct Answer: ");
	    										BufferedReader in5 = new BufferedReader(new InputStreamReader(System.in));
	    										int input = in5.read();
	    										stmt.executeUpdate("UPDATE assessments SET PointsPerQuestion='"+input+"' WHERE ProfessorID='"+userID+"', AssignmentID='"+editHomework+"'");
	    										break;
	    									case 7://points per incorrect answer
	    										System.out.println("Input New Points Per Correct Answer: ");
	    										BufferedReader in6 = new BufferedReader(new InputStreamReader(System.in));
	    										int inputt = in6.read();
	    										stmt.executeUpdate("UPDATE assessments SET PointsOPIQ='"+inputt+"' WHERE ProfessorID='"+userID+"', AssignmentID='"+editHomework+"'");
	    										break;
	    									case 8://back
	    										break;
	    								}
	    								break;
	    							case 3://add questions
	    								rs = stmt.executeQuery("SELECT Topics FROM courses WHERE CourseID='"+courseID+"'");
	    								
	    								
	    								String qTopics = rs.getString("Teaches");
	    	    						char[] top = qTopics.toCharArray();
	    	    						j=0;
	    	    						int numOfTopics=0;
	    	    						int[] indexs = new int[10];
	    	    						for(i=0; i<qTopics.length(); i++){//count number of classes enrolled in
	    	    							if(i==0&&top[0]==',')
	    	    								i++;
	    	    							if(top[i]==','){
	    	    								numOfClasses++;
	    	    								indexs[j]=i;
	    	    								j++;
	    	    							}
	    	    						}
	    	    						if(numOfTopics==0){//not enrolled in any courses
	    	    							System.out.println("There aren't any registered topics for this class");
	    	    							break;
	    	    						}
	    	    						j=0;
	    	    						int h=0;
	    	    						String[] topi = new String[numOfTopics];
	    	    						for(i=0; i<numOfTopics; i++){//makes strings of class ids
	    	    							topi[j]=qTopics.substring(h, index[i]-1);
	    	    							h=index[i]+1;
	    	    							j++;
	    	    						}
	    	    						String[] topics = new String[numOfTopics];
	    	    						for(i=0; i<numOfTopics; i++){
	    	    							rs = stmt.executeQuery("SELECT CoursesName FROM courses WHERE CourseID='"+crass[i]+"'");
	    	    							topics[i] = rs.getString("CoursesName");
	    	    						}
	    	    						int[] topicss = new int[numOfTopics];
	    	    						for(i=0;i<topicss.length; i++){
	    	    							topicss[i]=Integer.parseInt(topics[i]);//ids for topics for the class
	    	    						}
	    								
	    	    						System.out.println("What Topic does this question belong too?: ");
	    	    						for(i=0; i<topicss.length; i++){
	    	    							rs = stmt.executeQuery("SELECT name FROM topics WHERE TopicID='"+topicss[i]+"'");
	    	    							topics[i] = rs.getString("name");
	    	    							System.out.println((i+1)+". "+topics[i]);
	    	    						}
	    								//printed out all options
	    	    						BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	    								int userTopic = topicss[in.read()-1];
	    								
	    								
	    								System.out.println("Input New Question: ");
										String question = in.readLine();
										stmt.executeUpdate("INSERT INTO Questions VALUES(4, 1, "+userTopic+", '"+question+"','Q4')");
	    								break;
	    							case 4://add answer
	    								
	    								break;
	    							case 5://reports
	    								
	    								break;
	    							case 6://go back
	    								break;
	    						}
	    					}
	    					
	    					
	    					
	    					
	    					break;
	    				case 2://add a course page
	    					String token = addCourse();
	    					rs = stmt.executeQuery("SELECT CourseID, "
	    							+"EndDate FROM courses WHERE Token='"+token+"'");
	    					String a = rs.getString("CourseID");
	    					String b = rs.getString("EndDate");
	    					
	    					if(a!=""){
	    						int currYear = Integer.parseInt(currentDate.substring(0, 4));
		    					int classYear = Integer.parseInt(b.substring(0, 4));
		    					int currMonth = Integer.parseInt(currentDate.substring(5, 7));
		    					int classMonth = Integer.parseInt(b.substring(5, 7));
		    					int currDay = Integer.parseInt(currentDate.substring(9));
		    					int classDay = Integer.parseInt(b.substring(9));
		    					
		    					if(currYear<classYear){
		    						//add
		    						if(userType=="student"){
		    							rs = stmt.executeQuery("SELECT Students"
		    									+"FROM courses WHERE CourseID='"+a+"'");
		    							String coursesStudents = rs.getString("Students");
		    							stmt.executeUpdate("UPDATE courses SET Students='"+coursesStudents+","+userID+"' WHERE CoursesID='"+a+"'");
		    							rs = stmt.executeQuery("SELECT enrolledIn FROM student WHERE studentID='"+userID+"'");
		    							coursesStudents = rs.getString("enrolledIn");
		    							stmt.executeUpdate("UPDATE student SET enrolledIn='"+coursesStudents+","+a+"' WHERE studentID='"+userID+"'");
		    						} else if(userType=="professor"){
		    							stmt.executeUpdate("UPDATE courses SET ProfessorID='"+userID+"' WHERE CourseID='"+a+"'");//professor is now the professor of the course
		    							rs = stmt.executeQuery("SELECT Teaches FROM Professor WHERE ProfessorID="+userID+"'");
		    							String coursesPro = rs.getString("Teaches");
		    							stmt.executeUpdate("UPDATE Professor SET Teaches='"+coursesPro+","+a+"' WHERE ProfessorID='"+userID+"'");
		    						}//else if usertype=ta
		    						
		    					} else if(currYear==classYear){
		    						if(currMonth<=classMonth){
		    							if(currDay<=classDay){
		    								//add
		    								if(userType=="student"){
				    							rs = stmt.executeQuery("SELECT Students"
				    									+"FROM courses WHERE CourseID='"+a+"'");
				    							String coursesStudents = rs.getString("Students");
				    							stmt.executeUpdate("UPDATE courses SET Students='"+coursesStudents+","+userID+"' WHERE CoursesID='"+a+"'");
				    							rs = stmt.executeQuery("SELECT enrolledIn FROM student WHERE studentID='"+userID+"'");
				    							coursesStudents = rs.getString("enrolledIn");
				    							stmt.executeUpdate("UPDATE student SET enrolledIn='"+coursesStudents+","+a+"' WHERE studentID='"+userID+"'");
				    						} else if(userType=="professor"){
				    							stmt.executeUpdate("UPDATE courses SET ProfessorID='"+userID+"' WHERE CourseID='"+a+"'");//professor is now the professor of the course
				    							rs = stmt.executeQuery("SELECT Teaches FROM Professor WHERE ProfessorID="+userID+"'");
				    							String coursesPro = rs.getString("Teaches");
				    							stmt.executeUpdate("UPDATE Professor SET Teaches='"+coursesPro+","+a+"' WHERE ProfessorID='"+userID+"'");
				    						}
		    							} else {
		    								//past date
		    								System.out.println("Course Over, Cannot Register");
		    								break;
		    							}
		    						} else {
		    							//past date
		    							System.out.println("Course Over, Cannot Register");
	    								break;
		    						}
		    					} else if(currYear>classYear){
		    						//past date
		    						System.out.println("Course Over, Cannot Register");
    								break;
		    					}
	    					} else {
	    						//incorrect token
	    						System.out.println("Invalid Token");
	    						break;
	    					}
	    					
	    					break;
	    				case 3://go back to start menu
	    					break;
					}
					break;
				
			case 2://create user page
	    		String[] newUser = newUser();
	    		if(newUser[0]=="student"){
	    			stmt.executeUpdate("INSERT INTO student " +
	    					   "VALUES ('"+newUser[2]+"', '', '"+newUser[1]+"')");
	    		} if(newUser[0]=="professor"){
	    			stmt.executeUpdate("INSERT INTO Professor " +
	    					   "VALUES ('', '"+newUser[2]+"', '"+newUser[1]+"')");
	    		}
	    		System.out.println("User added\n");
	    		break;
	    	
			case 3://exit program
	    		close(rs);
	            close(stmt);
	            close(conn);
	    		System.exit(0);
	    		break;
	    	}
		}
		
		
		
        } finally {
            close(rs);
            close(stmt);
            close(conn);
          }
        } catch(Throwable oops) {
            oops.printStackTrace();
        }
	}
	
	static void close(Connection conn) {
        if(conn != null) {
            try { conn.close(); } catch(Throwable whatever) {}
        }
    }

    static void close(Statement st) {
        if(st != null) {
            try { st.close(); } catch(Throwable whatever) {}
        }
    }

    static void close(ResultSet rs) {
        if(rs != null) {
            try { rs.close(); } catch(Throwable whatever) {}
        }
    }
    
    /**
     * Displays a list of assignment names to be chosen by the user
     * @param names
     * @return courseID choose
     * @throws IOException
     */
    public static String displayHomework(String[] names) throws IOException{
    	String choice = "";
    	System.out.println("Choose Assessment to Edit: ");
    	for(int i=0; i<names.length; i++){
    		System.out.println((i+1)+". "+names[i]);
    	}
    	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    	choice = names[in.read()-1];
    	
    	return choice;
    }
    
    /**
     * Gets new user information
     * @return array of new user name and password
     * array[0]=user type, array[1]=user name, array[2]=user password
     * @throws IOException
     */
    public static String[] newUser() throws IOException{//so far ignores TA's
    	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    	String[] input = new String[3];
    	System.out.println("Is the new user a student or professor? (Enter 'student' or 'professor'): ");
    	input[0]=in.readLine();
    	System.out.println("Enter New User Name: ");
    	input[1]=in.readLine();
    	System.out.println("Enter New User Password: ");
    	input[2]=in.readLine();
    	
    	return input;
    }
    
    /**
     * Prompts the user to either login, create a new user, or exit the program and returns the integer value
     * coordinated with their choice.
     * @return choice the user made
     * 1=login, 2=create user, 3=exit program
     * @throws IOException
     */
    public static int start() throws IOException{
    	System.out.println("1. Login\n2. Create User\n3. Exit\n(Enter 1, 2, or 3)");
    	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    	String input = in.readLine();
    	while(input=="1" ^ input=="2" ? input=="3" : input=="1"){
    		System.out.println("Please enter '1' for Login, '2' for Create User, or '3' for Exit as an input");
    		input = in.readLine();
    	}
    	int choice = Integer.parseInt(input);
    	
    	return choice;
    	/*
    	switch(choice){
    	case 1:
    		
    		break;
    	case 2:
    		
    		break;
    	case 3:
    		
    		break;
    	}*/
    }
    
    /**
     * Prompts the user to enter their name and password and returns them in an array
     * @return An array of strings containing the user name and user password
     * array[0]=user name, array[1]=user password
     * @throws IOException
     */
    public static String[] login() throws IOException{
    	System.out.print("Enter User ID: ");
    	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    	String user = in.readLine();
    	System.out.print("\nEnter User Password: ");
    	String password = in.readLine();
    	String[] login = new String[2];
    	login[0]=user;
    	login[1]=password;
    	return login;
    }
    
    /**
     * Prompts the user to enter an integer for control flow.
     * @return The action the user wants
     * 1=select a course, 2=add a course, 3=go back
     * @throws IOException
     */
    public static int loggedInChooseCourse() throws IOException{
    	System.out.println("1. Select Course\n2. Add Course\n3. Back\n(Enter 1, 2, or 3)");
    	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    	String input = in.readLine();
    	while(input=="1" ^ input=="2" ? input=="3" : input=="1"){
    		System.out.println("Please enter '1' for Course Selection, '2' for Add Course, or '3' for to go Back as an input");
    		input = in.readLine();
    	}
    	int choice = Integer.parseInt(input);
    	
    	return choice;
    }
    
    /**
     * Asks the user for the course token
     * @return token for course
     * @throws IOException
     */
    public static String addCourse() throws IOException{
    	System.out.println("Enter Course Token: ");
    	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    	String temp = in.readLine();
    	return temp;
    }
    
    /**
     * Takes a list of strings as courses and prints them as options for students to choose from.
     * Returns the name of the course the user wants or a null string for the choice to go pack to main menu.
     * @param courses
     * @return name of course the user wants
     * @throws IOException
     */
    public static String selectCourse(String[] courses, String[] names) throws IOException{
    	int length = courses.length;
    	System.out.println("Select course:");
    	int i;
    	for(i=0; i<length; i++){
    		System.out.println((i+1)+". "+courses[i]+" - "+names[i]);
    	}
    	System.out.println((i)+". Back");
    	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    	int course = in.read();
    	if(course<=length){
    		String choice = courses[course-1];
    		return choice;
    	} else{
    		//want to go back
    		return "";
    	}
    }
    
    /**
     * Prompts the student will their control options and returns their response
     * @return choice of student
     * 1=view scores, 2=attempt homework, 3=view past submissions, 4=back
     * @throws IOException
     */
    public static int getCourseOptionsStu() throws IOException{
    	System.out.println("1. View Scores\n2. Attempt Homework\n3. View Past Submissions\n4. Back\n(Enter 1, 2, 3, or 4)");
    	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    	String input = in.readLine();
    	while(true){
    		if(Integer.parseInt(input)==1)
    			break;
    		if(Integer.parseInt(input)==2)
    			break;
    		if(Integer.parseInt(input)==3)
    			break;
    		if(Integer.parseInt(input)==4)
    			break;
    		System.out.println("Invalid Option");
    		input = in.readLine();
    	}
    	int choice = Integer.parseInt(input);
    	
    	return choice;
    }
    
    /**
     * Prompts the professor their control options and returns the answer
     * @return choice of the professor
     * 1=add homework, 2=edit homework, 3=add question, 4=add answer, 5=reports, 6=back
     * @throws IOException
     */
    public static int getCourseOptionsPro() throws IOException{
    	System.out.println("1. Add Homework\n2. Edit Homework\n3. Add Question\n4. Add Answer\n5. Reports\n6. Back");
    	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    	String input = in.readLine();
    	while(true){
    		if(Integer.parseInt(input)==1)
    			break;
    		if(Integer.parseInt(input)==2)
    			break;
    		if(Integer.parseInt(input)==3)
    			break;
    		if(Integer.parseInt(input)==4)
    			break;
    		if(Integer.parseInt(input)==5)
    			break;
    		if(Integer.parseInt(input)==6)
    			break;
    		System.out.println("Invalid Option");
    		input = in.readLine();
    	}
    	int choice = Integer.parseInt(input);
    	
    	return choice;
    }
    
    /**
     * This method gets the parameters for assessments (create homework/update homework) from the professor's inputs
     * @return Array of Strings for each assignment parameters
     *  array[0]=start date, array[1]=end date, array[2]=number of attempts,
     *  array[3]=score selection scheme, array[4]=number of questions,
     *  array[5]=points per correct answer, array[6]=points per incorrect answer, array[7]=assignment name
     * @throws IOException
     */
    public static String[] getNewAssignmentInfo() throws IOException{
    	String[] homework = new String[8];
    	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    	System.out.println("Input Start Date (YYYY-MM-DD): ");
    	homework[0]=in.readLine();//start date
    	System.out.println("\nInput End Date (YYYY-MM-DD): ");
    	homework[1]=in.readLine();//end date
    	System.out.println("\nInput Number of Attempts Allowed: ");
    	homework[2]=in.readLine();//number of attempts
    	System.out.println("\nScore Selection Scheme: ");
    	homework[3]=in.readLine();//score selection scheme
    	System.out.println("\nNumber of Questions: ");
    	homework[4]=in.readLine();//number of questions
    	System.out.println("\nPoints Possible for Correct Answers: ");
    	homework[5]=in.readLine();//correct answer points
    	System.out.println("\nPoints to Be Reduced From Incorrect Answers: ");
    	homework[6]=in.readLine();//incorrect answer points
    	System.out.println("\nAssignment name: ");
    	homework[7]=in.readLine();//name
    	
    	return homework;
    }
    
    /**
     * Gets the choice of which new information the professor wishes to update per assignment
     * @return an integer correlating with an instruction
     * 1=start date, 2=end date, 3=number of attempts, 4=score selection,
     * 5=number of questions, 6=number of points per correct answer, 7=number of points per incorrect answer,
     * 8=back
     * @throws IOException
     */
    public static int getEditChoice() throws IOException{
    	System.out.println("1. Start Date\n2. End Date\n3. Number of Attempts\n4. Score Selection\n5. Number of Questions\n6. Number of Points per Correct Question\n7. Number of Points per Incorrect Question\n8. Back");
    	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    	String input = in.readLine();
    	while(true){
    		if(Integer.parseInt(input)==1)
    			break;
    		if(Integer.parseInt(input)==2)
    			break;
    		if(Integer.parseInt(input)==3)
    			break;
    		if(Integer.parseInt(input)==4)
    			break;
    		if(Integer.parseInt(input)==5)
    			break;
    		if(Integer.parseInt(input)==6)
    			break;
    		if(Integer.parseInt(input)==7)
    			break;
    		if(Integer.parseInt(input)==8)
    			break;
    		System.out.println("Invalid Option");
    		input = in.readLine();
    	}
    	int choice = Integer.parseInt(input);
    	
    	return choice;
    }
    
    /**
     * Gets a new question for an assignment and one correct answer and 3 incorrect answers
     * @return array of strings containing:
     * array[0]=new question, array[1]=correct answer, array[2]=first incorrect answer,
     * array[3]=second incorrect answer, array[4]=third incorrect answer
     * @throws IOException
     */
    public static String[] getNewQuestionInfo() throws IOException{
    	String[] question = new String[5];
    	System.out.println("Enter question, correct answer , 1st incorrect answer, 2nd incorrect answer, 3rd incorrect answer separated by newline");
    	BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    	question[0]=in.readLine();
    	question[1]=in.readLine();
    	question[2]=in.readLine();
    	question[3]=in.readLine();
    	question[4]=in.readLine();
    	
    	return question;
    }
}
